﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace StarlightUI
{
    public partial class ContentExplorer : UserControl
    {
        public class ContentEntry
        {
            public string Path = string.Empty;
            public int X, Y;
            public int Width=128, Height=128;
        }
        public class ContentFolder : ContentEntry
        {
            public ContentFolder(string path)
            {
                Path = path;
            }
        }
        public class ContentFile : ContentEntry
        {
            public ContentFile(string path)
            {
                Path = path;
            }
        }

        [System.ComponentModel.Browsable(true),
System.ComponentModel.Category("Presentation"),
System.ComponentModel.Description("Background image for the explorer. 100% cosmetic.")]
        public System.Drawing.Image BackgroundImg
        {
            get
            {
                return mBack;
            }
            set
            {
                mBack = value;
                this.Invalidate();
            }
        }
        private System.Drawing.Image mBack = null;
       

        public ContentExplorer()
        {
            InitializeComponent();
           // SystemFolder = Image.FromFile("Icons/SystemFolder.png");

        }
        [System.ComponentModel.Browsable(true),
System.ComponentModel.Category("Icons"),
System.ComponentModel.Description("Icon for generic non-specialized folders of varied/no content.")]
        public Image SystemFolder
        {
            get
            {
                return mSystemFolder;
            }
            set
            {
                mSystemFolder = value;
            }
        }
        private Image mSystemFolder = null;

        [System.ComponentModel.Browsable(true),
System.ComponentModel.Category("Icons"),
System.ComponentModel.Description("Icon for generic system file of no special meaning to the engine/editor.")]   
        public Image SystemFile
        {
            get
            {
                return mSystemFile;
            }
            set
            {
                mSystemFile = value;
            }
        }
        private Image mSystemFile = null;

        public int IconWidth
        {
            get
            {
                return mIconWidth;
            }
            set
            {
                mIconWidth = value;
            }
        }
        private int mIconWidth = 128;
        public int IconHeight
        {
            get
            {
                return mIconHeight;
            }
            set
            {
                mIconHeight = value;
            }
        }
        private int mIconHeight = 128;
        public int SpaceBetweenIcons
        {
            get
            {
                return mSpace;
            }
            set
            {
                mSpace = value;
                SyncPositions();
            }
        }
        private int mSpace = 10;
        public void SyncPositions()
        {
            int ax = 5;
            int ay = 25;
            foreach (var c in Content)
            {
                c.X = ax;
                c.Y = ay;
                ax += IconWidth + SpaceBetweenIcons;
                if (ax >= (Width - IconWidth - 5))
                {
                    ax = 5;
                    ay += IconHeight + SpaceBetweenIcons;
                }
            }
        }
        public float ViewY
        {
            get
            {
                return mViewY;
            }
            set
            {
                mViewY = value;
                if (mViewY < 0) mViewY = 0;
            }
        }
        private float mViewY = 0;
        public void ScanContent(string path)
        {
           
            ContentPath.Text = path;
            DirectoryInfo info = new DirectoryInfo(path);
            foreach (var fi in info.GetDirectories())
            {
                ContentFolder cf = new ContentFolder(fi.FullName);
                Content.Add(cf);
             
            }
            foreach (var fi in info.GetFiles())
            {
                Content.Add(new ContentFile(fi.FullName));
            
            }
        }
        public List<ContentEntry> Content = new List<ContentEntry>();
        
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics graphics = e.Graphics;

            int penWidth = 4;
            Pen pen = new Pen(Color.Black, 4);

            int fontHeight = 10;
            Font font = new Font("Arial", fontHeight);

            if (mBack!=null)
            {
                graphics.DrawImage(mBack, 0, 0, Width, Height);
            }

        }
    }
}
